<?php
/**
 * Model.php
 * Created by PhpStorm.
 * User: 江小波
 * Date: 2019/6/5
 * Time: 9:23
 */
namespace app\common\simple_rbac;

use app\common\Config;
use app\common\Singleton;
use think\Db;

class Model
{
    use Singleton;

    // 表数组
    const TABLE_USER = 'simple_rbac_user';
    const TABLE_ROLE = 'simple_rbac_role';
    const TABLE_NODE = 'simple_rbac_node';

    // 当前操作数据库对象
    protected $connect;

    /**
     * Logic constructor.
     * @param null $connect
     */
    public function __construct()
    {
        // 实例化链接实例
        $this->connect = Db::connect(Config::SIMPLE_RBAC_DSN);
    }

    /**
     * 添加节点
     * @param $node
     * @return int|string
     */
    public function addNode($node)
    {
        return $this->connect->table(self::TABLE_NODE)->insertGetId($node);
    }
    /**
     * 添加角色
     * @param $node
     * @return int|string
     */
    public function addRole($role)
    {
        return $this->connect->table(self::TABLE_ROLE)->insertGetId($role);
    }
    /**
     * 添加用户
     * @param $node
     * @return int|string
     */
    public function addUser($user)
    {
        return $this->connect->table(self::TABLE_USER)->insertGetId($user);
    }

    /**
     * 更新节点
     * @param $where
     * @param $node
     * @return int|string
     */
    public function saveNode($where,$node)
    {
        return $this->connect->table(self::TABLE_NODE)->where($where)->update($node);
    }

    /**
     * 更新角色
     * @param $where
     * @param $role
     * @return int|string
     */
    public function saveRole($where,$role)
    {
        return $this->connect->table(self::TABLE_ROLE)->where($where)->update($role);
    }

    /**
     * 更新用户
     * @param $where
     * @param $user
     * @return int|string
     */
    public function saveUser($where,$user)
    {
        return $this->connect->table(self::TABLE_USER)->where($where)->update($user);
    }

    /**
     * 获取节点
     * @param $where
     * @param string $field
     * @return array|false|null|\PDOStatement|string|\think\Model
     */
    public function getNode($where,$field="*")
    {
        return $this->connect->table(self::TABLE_NODE)
            ->field($field)
            ->where($where)
            ->find();
    }

    /**
     * 获取角色列表
     * @param string $field
     * @return array|false|\PDOStatement|string|\think\Collection
     */
    public function getRoleList($field="*")
    {
        return $this->connect->table(self::TABLE_ROLE)
            ->field($field)
            ->where('status',1)
            ->where('platform_id',config('platform_info.id'))
            ->select();
    }

    /**
     * 获取节点列表
     * @param string $field
     * @return array|false|\PDOStatement|string|\think\Collection
     */
    public function getNodeList($field="*")
    {
        return $this->connect->table(self::TABLE_NODE)
            ->field($field)
            ->select();
    }

    /**
     * 根据用户名获取用户详情
     * @param $username
     * @return array|false|null|\PDOStatement|string|\think\Model
     */
    public function getUserByUsername($username)
    {
        return $this->connect->table(self::TABLE_USER)
            ->where('username',$username)
            ->find();
    }

    /**
     * 是否是超级管理角色
     * @param $roleIds
     * @return bool
     */
    public function checkSuperAdminByRoles($role_ids)
    {
        $superCount = $this->connect->table(self::TABLE_ROLE)
            ->whereIn('id',$role_ids)
            ->where('name',config('platform_info.super_admin'))
            ->where('platform_id',config('platform_info.id'))
            ->count();
        return $superCount > 0;
    }

    /**
     * 根据用户IDS获取权限节点ID列表
     * @param $user_ids
     * @return array
     */
//    public function getAccessNodesByUserIds($user_ids)
//    {
//        $list = $this->connect->table(self::TABLE_USER)
//            ->field('nodes')
//            ->whereIn('id',$user_ids)
//            ->where('lock',0)
//            ->select();
//        return $this->getNotRepeatNodes(array_map('array_shift',$list));
//    }

    /**
     * 根据角色IDS获取权限节点ID列表
     * @param $role_ids
     * @return array
     */
    public function getAccessNodesByRoleIds($role_ids)
    {
        $list = $this->connect->table(self::TABLE_ROLE)
            ->field('nodes')
            ->whereIn('id',$role_ids)
            ->where('status',1)
            ->select();
        return $this->getNotRepeatNodes(array_map('array_shift',$list));
    }

    /**
     * 获取不重复节点
     * @param $nodes
     * @return array
     */
    public function getNotRepeatNodes($nodes)
    {
        $res = [];
        foreach ($nodes as &$val)
        {
            if(!empty($val)){
                $res = array_merge($res,explode(",",trim($val,",")));
            }
        }
        // 去除重复的值，重新排列索引返回值
        return array_values(array_unique($res));
    }
}